﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarginTrading.Services;
using MarginTrading.Entity;
using MarginTrading.Common;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Globalization;

namespace MarginTrading.site
{
    public partial class DebtorsFollowUp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserType"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

            if (!IsPostBack)
            {
                DataTable advisor = AgentServices.GetAgentCollectionDataTable();
                DataRow dr = advisor.NewRow();
                dr["NAME"] = "All";
                dr["CODE"] = "All";
                advisor.Rows.InsertAt(dr, 0);
                DataView dv = new DataView(advisor);
                ddlAgent.DataSource = advisor;
                ddlAgent.DataTextField = "NAME";
                ddlAgent.DataValueField = "CODE";
                ddlAgent.DataBind();
            }
        }

        protected void SearchBtn_Click(object sender, EventArgs e)
        {
            DateTime date = DateTime.MinValue;
            DateTime.TryParseExact(txtDate.Text, "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
            LoadReport(ddlAgent.Text, date);
        }

        private void LoadReport(string agentCode, DateTime date)
        {
            try
            {
                Agent agent = AgentServices.GetAgentByCode(agentCode);

                if (agent == null)
                {
                    agent = new Agent();
                }
                DataTable dates = null;
                if (agentCode != "All")
                {
                    dates = ReportServices.GetTop3Days(agentCode, date);
                }
                else
                {
                    dates = ReportServices.GetTop3Days(date);
                    agent.Name = "All";
                }
                DateTime date1 = DateTime.MinValue;
                DateTime date2 = DateTime.MinValue;
                DateTime date3 = DateTime.MinValue;


                if (dates.Rows.Count > 0)
                {
                    DateTime.TryParse(dates.Rows[0][0].ToString(), out date1);
                }
                if (dates.Rows.Count > 1)
                {
                    DateTime.TryParse(dates.Rows[1][0].ToString(), out date2);
                }
                if (dates.Rows.Count > 2)
                {
                    DateTime.TryParse(dates.Rows[2][0].ToString(), out date3);
                }

                DateTime date4 = ReportServices.GetSettlementDate(date1);
                DateTime date5 = ReportServices.GetSettlementDate(date2);
                DateTime date6 = ReportServices.GetSettlementDate(date3);

                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";
                if (agentCode != "All")
                {
                    ds.Value = ReportServices.GetDebtorsFollowupByAgentAndDate(agentCode, date1, date2, date3);
                }
                else
                {
                    ds.Value = ReportServices.GetDebtorsFollowupByAgentAndDate(date1, date2, date3);
                }

                this.ReportViewer1.LocalReport.ReportPath = @"Reports\DebtorsFollowup.rdlc";
                this.ReportViewer1.LocalReport.DataSources.Clear();
                this.ReportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()), 
                        new ReportParameter("Date1",date1 == DateTime.MinValue ? "" : date1.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("Date2",date2 == DateTime.MinValue ? "" : date2.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("Date3",date3 == DateTime.MinValue ? "" : date3.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("Date4",date4 == DateTime.MinValue ? "" : date4.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("Date5",date5 == DateTime.MinValue ? "" : date5.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("Date6",date6 == DateTime.MinValue ? "" : date6.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("AgentName",agent.Name ),
                        new ReportParameter("ReportTitle",Reports.DEBTORS_FOLLOW_UP_LIST)});

                //this.ReportViewer1.RefreshReport();
                //ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                //ReportViewer1.ZoomMode = ZoomMode.Percent;
                //ReportViewer1.ZoomPercent = 100;

            }
            catch (System.Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }
    }
}
